package uutalk.netcloud.talk.app.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.fastjson.JSON;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;
import uutalk.netcloud.talk.R;
import uutalk.netcloud.talk.app.ui.Activity.CreateActActivity;
import uutalk.netcloud.talk.app.ui.Activity.ScanJoinActivity;
import uutalk.netcloud.talk.app.ui.adapter.GroupListAdapter;
import uutalk.netcloud.talk.base.android.BaseFragment;
import uutalk.netcloud.talk.base.mqtt.MQTTManager;
import uutalk.netcloud.talk.base.mqtt.PublishFactory;
import uutalk.netcloud.talk.base.mqtt.event.MqttMessageEvent;
import uutalk.netcloud.talk.base.mqtt.model.Publication;
import uutalk.netcloud.talk.base.util.LanguageUtil;
import uutalk.netcloud.talk.base.util.thirdpart.UserManager;
import uutalk.netcloud.talk.base.util.view.ToastUtil;
import uutalk.netcloud.talk.model.BussinessBeanFactory;
import uutalk.netcloud.talk.model.bean.CMDConstant;
import uutalk.netcloud.talk.model.bean.GroupListWrap;

/**
 * 类描述：
 * 作者：Pengmutian
 * 时间：2018/5/12
 */
public class GroupListFragment extends BaseFragment {

    String userID = "";

    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;

    public static GroupListFragment newInstance() {
        Bundle args = new Bundle();
        GroupListFragment fragment = new GroupListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int setLayoutResId() {
        return R.layout.fragment_group_list;
    }

    @Override
    protected void loadData() {

    }

    @Override
    public boolean supportActionbar() {
        return true;
    }

    @Override
    public boolean isAutoBindEvent() {
        return true;
    }

    @Override
    protected void init() {
        getTitleTV().setText(LanguageUtil.getStringById(R.string.group_talk));
        userID = UserManager.getInstance().getUserInfo().getUser_id();
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity, LinearLayoutManager.VERTICAL, false));
        recyclerView.addItemDecoration(new DividerItemDecoration(mActivity, DividerItemDecoration.VERTICAL));

        if (MQTTManager.getInstance().isConnected()) {
            progressHudUtil.showProgressHud();
            Publication publication = PublishFactory.createPublication(BussinessBeanFactory.createJoinedGroups());
            MQTTManager.getInstance().publish(publication);
        } else {
            ToastUtil.makeToast(LanguageUtil.getStringById(R.string.get_data_erro));
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMqttMsgEvent(MqttMessageEvent event) {
        if (CMDConstant.GET_USER_JOINED_GROUPS_RESP.equals(event.getMessage().getCmd())) {
            progressHudUtil.dismissProgressHud();
            //获取加入过的群组
            GroupListWrap listWrap = JSON.parseObject(event.getMessage().toString(), GroupListWrap.class);
            if (listWrap.isOk()) {
                GroupListAdapter groupListAdapter = new GroupListAdapter(listWrap.getGroup_list());
                recyclerView.setAdapter(groupListAdapter);
            } else {
                ToastUtil.makeToast(listWrap.error_msg);
            }
        }
    }

    @OnClick({R.id.btnAddGroup, R.id.btnCreateGroup})
    public void onViewClick(View view){
        switch (view.getId()){
            case R.id.btnAddGroup:{
                Intent intent = new Intent(mActivity, ScanJoinActivity.class);
                startActivity(intent);
            } break;
            case R.id.btnCreateGroup:{
                //游客不能创建活动
                if (UserManager.getInstance().getLoginInfo().getUserType().equals("2")) {
                    ToastUtil.makeToast(LanguageUtil.getStringById(R.string.no_permission_create));
                    return;
                }
                Intent intent = new Intent(mActivity, CreateActActivity.class);
                startActivity(intent);
            } break;
        }
    }
}
